<?php

class PageController
{
    private $pageModel;
    private $pageView;
    
    public function __construct()
    {
        require_once 'PageModel.php';
        $this->pageModel = new PageModel();
    
        require_once 'PageView.php';
        $this->pageView = new PageView();
    }
    
    public function DoControll()
    {
        $pageData = $this->pageModel->GetPageData();
        
        if ( $this->pageView->TriedToAddSection() )
        {
            $this->pageModel->AddSection( $pageData['id'], $this->pageView->GetAddSectionWidth() );
        }

        if ( $this->pageView->GetSectionOptionsOption() == "Delete Section" )
        {
            $this->pageModel->DeleteSection( $pageData['id'], $this->pageView->GetSectionOptionSectionID() );
        }
        
        if ( $this->pageView->GetEditSectionOption() == "Save Content" )
        {
            $this->pageModel->EditSection( $this->pageView->GetEditSectionSectionID(), $this->pageView->GetEditSectionWidth(), $this->pageView->GetEditSectionContent() );
        }
        
        $html = '';
        
        $themeContent = $this->pageModel->GetThemeContent();
        
        $themeContent = preg_replace_callback( '/\[Page:([a-z0-9]+)\]/i', array( $this, 'PageShortcode' ), $themeContent );
        
        if ( preg_match( "/<!-- START EDITOR -->/i", $themeContent ) )
        {
            $part1 = substr( $themeContent, 0, strpos( $themeContent, "<!-- START EDITOR -->" ) );
            $part2 = substr( $themeContent, strpos( $themeContent, "<!-- START EDITOR -->" ), strpos( $themeContent, "<!-- END EDITOR -->" ) - strpos( $themeContent, "<!-- START EDITOR -->" ) );
            $part3 = substr( $themeContent, strpos( $themeContent, "<!-- END EDITOR -->" ) + 19 );
            
            $part1 = preg_replace_callback( '/\[Page:([a-z0-9]+)\]/i', array( $this, 'PageShortcode' ), $part1 );
            $part1 = preg_replace_callback( '/\[Website:([a-z0-9]+)\]/i', array( $this, 'WebsiteShortcode' ), $part1 );
            $part1 = preg_replace_callback( '/\[Menu:([a-z0-9]+)\]/i', array( $this, 'MenuShortcode' ), $part1 );
            $part1 = preg_replace_callback( '/\[Plugin:([a-z0-9]+)\]/i', array( $this, 'PluginShortcode' ), $part1 );
            
            $part3 = preg_replace_callback( '/\[Page:([a-z0-9]+)\]/i', array( $this, 'PageShortcode' ), $part3 );
            $part3 = preg_replace_callback( '/\[Website:([a-z0-9]+)\]/i', array( $this, 'WebsiteShortcode' ), $part3 );
            $part3 = preg_replace_callback( '/\[Menu:([a-z0-9]+)\]/i', array( $this, 'MenuShortcode' ), $part3 );
            $part3 = preg_replace_callback( '/\[Plugin:([a-z0-9]+)\]/i', array( $this, 'PluginShortcode' ), $part3 );
            
            $themeContent = $part1 . $part2 . $part3;
        }
        else
        {
            $themeContent = preg_replace_callback( '/\[Page:([a-z0-9]+)\]/i', array( $this, 'PageShortcode' ), $themeContent );
            $themeContent = preg_replace_callback( '/\[Website:([a-z0-9]+)\]/i', array( $this, 'WebsiteShortcode' ), $themeContent );
            $themeContent = preg_replace_callback( '/\[Menu:([a-z0-9]+)\]/i', array( $this, 'MenuShortcode' ), $themeContent );
            $themeContent = preg_replace_callback( '/\[Plugin:([a-z0-9]+)\]/i', array( $this, 'PluginShortcode' ), $themeContent );
        }
        
        $html .= $themeContent;
        
        return $html;
    }
    
    private function PageShortcode( $matches )
    {
        $pageData = $this->pageModel->GetPageData();
        
        switch ( strtolower( $matches[1] ) )
        {
            case "title" :
                return $pageData['title'];
            case "content" :
                $editSection = ( $this->pageView->GetSectionOptionsOption() === "Edit Content" ) ? $this->pageView->GetSectionOptionSectionID() : false;
                return $this->pageView->DoSections( $pageData['sections'], $this->pageModel->GetSectionElements(), $editSection );
        }
        
        return "&raquo; INVALID SHORTCODE {$matches[0]} &laquo;";
    }
    
    private function WebsiteShortcode( $matches )
    {
        switch ( strtolower( $matches[1] ) )
        {
            case "name" :
                return Settings::WebsiteName();
        }
    
        return "&raquo; INVALID SHORTCODE {$matches[0]} &laquo;";
    }
    
    private function MenuShortcode( $matches )
    {
        require_once 'MenuController.php';
    
        $menuController = new MenuController( $matches[1] );
    
        $menuHtml = $menuController->DoControll();
    
        if ( strlen( trim( $menuHtml ) ) > 1 )
        {
            return $menuHtml;
        }
    
        return "&raquo; INVALID SHORTCODE {$matches[0]} &laquo;";
    }
    
    private function PluginShortcode( $matches )
    {
        $pluginFileName = "plugin/{$matches[1]}/{$matches[1]}.php";
        if ( file_exists( $pluginFileName ) )
        {
            require_once $pluginFileName;
            
            $className = '\\' . $matches[1] . '\\' . $matches[1];
            
            if ( class_exists( $className ) )
            {
                $plugin = new $className();
                
                if ( method_exists( $plugin, "RunPlugin" ) )
                {
                    return $plugin->RunPlugin();
                }
            }
        }
        
        return "&raquo; INVALID SHORTCODE {$matches[0]} &laquo;";
    }
}
